﻿using LxCFD;
using Prism.Mvvm;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media.Media3D;


namespace TimeSolve
{
    public partial class MeshVM
    {
        public class ZoneVM : BindableBase
        {
            public ZoneVM(FvZone zone)
            {
                this.zone = zone;
                foreach (var b in zone.BoundThreads)
                {
                    ThreadVM t = new ThreadVM(this, b);
                    BoundThreads.Add(t);
                }
            }
            FvZone zone;
            #region 头
            public string name { get => zone.name; set { zone.name = value; RaisePropertyChanged("name"); } }
            public int ID { get => zone.ID; set { zone.ID = value; RaisePropertyChanged("ID"); } }
            #endregion
            #region 几何模型
            public ObservableCollection<ThreadVM> BoundThreads { get; private set; } = new ObservableCollection<ThreadVM>();

            #endregion
            #region 物理模型设置
            public ContinuumVM continuum
            {
                get => _continuum;
                set
                {
                    _continuum = value;  
                    zone.SetPhysicalModel(_continuum.physicalModel);
                    continuumChanged();
                    RaisePropertyChanged("continuum");
                }
            }
            private ContinuumVM _continuum;//指向共有的模型
            public void continuumChanged()
            {
                foreach (var t in BoundThreads)
                {
                    t.updateAvailableBoundaryTypes();
                }
            }

            public double energySource
            {
                get
                {
                    if (zone.EnergySource is Value1Const.Double vc)
                        return vc.Number;
                    else
                        return 0;
                }
                set
                {
                    zone.EnergySource = new Value1Const.Double(value);
                    RaisePropertyChanged("energySource");
                }
            }
            #endregion
        }
    }
}
//public IndexNumberRedBlack InnerIndex { get; set; }
//public IndexNumber TotalIndex { get; set; }
//#region 场

//public FieldsBase_InZone fields { get => _fields; set => _fields = value; }
//private FieldsBase_InZone _fields;

//#endregion
//#region 多重网格
//public AMGSolverInZone AMGSolver { get; set; }

////多重网格接口


//int[] IProvideZoneTopology.getCellFacesAsO(int CellIndex)
//{
//    return cells[CellIndex].CellFacesAsO;
//}

//int[] IProvideZoneTopology.getCellFacesAsN(int CellIndex)
//{
//    return cells[CellIndex].CellFacesAsN;
//}

//int IProvideZoneTopology.getFaceOwnerCellIndex(int FaceIndex)
//{
//    return faces[FaceIndex].OwnerCellIndex;
//}

//int IProvideZoneTopology.getFaceNeighborCellIndex(int FaceIndex)
//{
//    return faces[FaceIndex].NeighborCellIndex;
//}

//OwnerNeighborPair IProvideZoneTopology.getOwnerNeighborCellIndex(int FaceIndex)
//{
//    return faces[FaceIndex].getOwnerAndNeignborCellIndex();
//}

//List<IndexNumber> IProvideZoneTopology.getThreadIndexs()
//{
//    List<IndexNumber> l = new List<IndexNumber>();
//    foreach (Thread t in BoundThreads)
//    {
//        l.Add(t.ThreadIndex);
//    }
//    return l;
//}
//IndexNumber IProvideZoneTopology.getInnerIndexs()
//{
//    return InnerIndex;
//}

//#endregion
//public Point3DCollection nodes { get => _nodes; set => _nodes = value; }
//private Point3DCollection _nodes;
//public Face[] faces { get => _faces; set => _faces = value; }
//private Face[] _faces;
//public Cell[] cells { get => _cells; set => _cells = value; }
//private Cell[] _cells;
//public double[] cellCommonArray;
//public double[] faceCommonArray;
//public void creatZoneGeomAndData()
//{
//    creatCells();
//    calZoneGeometry();
//    cellCommonArray = new double[cells.Length];
//    faceCommonArray = new double[faces.Length];
//}
//private void creatCells()
//{
//    //创建cell中的face信息
//    int cellcount = TotalIndex.EndCellIndex + 1;// BoundThreads[BoundThreads.Count - 1].ThreadIndex.EndCellIndex + 1;

//    TempCell[] tempCells = new TempCell[cellcount];//innerCellCount + boundCellCount
//    for (int i1 = 0; i1 < tempCells.Length; i1++)
//    {
//        tempCells[i1].cellfaces = new List<int>();
//        tempCells[i1].cellRelated = new List<int>();
//        tempCells[i1].CellFacesAsO = new List<int>();
//        tempCells[i1].CellFacesAsN = new List<int>();
//    }
//    //添加CellFace
//    for (int i8 = 0; i8 < faces.Length; i8++)
//    {
//        tempCells[faces[i8].OwnerCellIndex].CellFacesAsO.Add(i8);
//        tempCells[faces[i8].NeighborCellIndex].CellFacesAsN.Add(i8);
//    }
//    //cell中的cellf由list型转array型
//    this.cells = new Cell[tempCells.Length];
//    for (int ii = 0; ii < cells.Length; ii++)
//    {
//        cells[ii].CellFacesAsO = (tempCells[ii].CellFacesAsO).ToArray();
//        cells[ii].CellFacesAsN = (tempCells[ii].CellFacesAsN).ToArray();
//    }
//    cellCommonArray = new double[cells.Length];
//}
//private void calZoneGeometry()
//{
//    ////计算面的几何信息
//    for (int i2 = 0; i2 < faces.Length; i2++)
//    {
//        faces[i2].calBasicGeometry(_nodes);
//    }
//    //计算体的几何信息
//    for (int i5 = 0; i5 < cells.Length; i5++)//遍历zone中的cell
//    {
//        cells[i5].calBasicGeometry(_faces);
//    }
//    //遍历face，把两侧的cell建立连接
//    int i6 = 0;
//    for (; i6 <= InnerIndex.EndFaceIndex; i6++)//innerCellCount
//    {
//        faces[i6].calGeometry_inner(_cells);
//    }
//    for (; i6 < faces.Length; i6++)
//    {
//        faces[i6].calGeometry_bound(_cells);
//    }
//    for (int i7 = 0; i7 < cells.Length; i7++)
//    {
//        cells[i7].calMatrix_LeastSquare(faces);
//    }
//}
//public void splidZoneIndex(int segNum)
//{
//    InnerIndex.Split(segNum);
//    InnerIndex.SplitIndexRedBlack(this, segNum);
//    TotalIndex.Split(segNum);
//    foreach (var t in BoundThreads)
//    {
//        t.ThreadIndex.Split(segNum);
//    }
//    AMGSolver.splitAMGIndex(segNum);
//}
//public struct TempCell
//{
//    public List<int> cellfaces { get; set; }
//    public List<int> cellRelated { get; set; }
//    public int ownerCellCount { get; set; }
//    public List<int> CellFacesAsO { get; set; }
//    public List<int> CellFacesAsN { get; set; }
//}
//public void applySettings()
//{
//    PhysicalModelTranslater.ToZoneFieldsAndApplySetting(ref _fields, continuum, this);
//}
//public void ReportSettingChanged()
//{
//    if (MainViewModel.mainViewModel.mesh.isInitialized)
//        MainViewModel.mainViewModel.mesh.isInitialized = false;
//}
//return new IndexNumber(0, innerCellCount - 1, 0, interiorFaceCount - 1);
//IndexNumber inum= new IndexNumber(t.StartCellIndex,t.EndCellIndex,t.StartFaceIndex,t.EndFaceIndex);
//l.Add(inum);
//int face_num = tempCells[ii].cellfaces.Count;
//cells[ii].CellFaces = new int[face_num];
//cells[ii].AdjacentCells = new int[face_num];
//for (int i2 = 0; i2 < face_num; i2++)
//{
//    cells[ii].CellFaces[i2] = tempCells[ii].cellfaces[i2];
//    cells[ii].AdjacentCells[i2] = tempCells[ii].cellRelated[i2];
//}
//cells[ii].OwnFaceCount = tempCells[ii].ownerCellCount;
////先添加owner
//for (int i8 = 0; i8 < faces.Length; i8++)
//{
//    tempCells[faces[i8].OwnerCellIndex].cellfaces.Add((i8));
//    tempCells[faces[i8].OwnerCellIndex].cellRelated.Add(faces[i8].NeighborCellIndex);
//}
////中间给出作为面owner的次数
//for (int i8 = 0; i8 < tempCells.Length; i8++)
//{
//    tempCells[i8].ownerCellCount = tempCells[i8].cellfaces.Count;
//}
////后添加neighbor
//for (int i8 = 0; i8 < faces.Length; i8++)
//{
//    tempCells[faces[i8].NeighborCellIndex].cellfaces.Add((i8));
//    tempCells[faces[i8].NeighborCellIndex].cellRelated.Add(faces[i8].OwnerCellIndex);
//}
//public bool isChanged
//{
//    get => _isChanged;
//    set
//    {
//        _isChanged = value;
//        if (_isChanged)
//        {
//            MainViewModel.mainViewModel.mesh.isInitialized = false;
//        }
//    }
//}
//private bool _isChanged = true;
//public int innerCellCount { get; set; }
//public int boundCellCount { get; set; }
//public int interiorFaceCount { get; set; }

//public struct Cell
//{
//    public CellFace[] cellFaces;
//}
//public struct CellFace
//{
//    public CellFace (int threadInZone,int faceInThread)
//    {
//        this.threadInZone = threadInZone;
//        this.faceInThread = faceInThread;
//    }
//    public int threadInZone;
//    public int faceInThread;
//}

//public Zone coupledZone { get=>_coupledZone; set { _coupledZone = value;RaisePropertyChanged("coupledZone"); } }
//public Thread coupledThreaad { get=>_coupledThread; set { _coupledThread = value;RaisePropertyChanged("coupledThreaad"); } }


//int totalCellCount = innerCellCount + boundCellCount;
//for (int i5 = innerCellCount; i5 < totalCellCount; i5++)
//{
//    cells[i5].Center = faces[cells[i5].cellFaces[0]].Center;//boundcell只有一个面
//}

// public MeshData meshData { get; set; }
//public ModelBase modelValue { get; set; }